package com.aaa.backsystem.controller;


import com.aaa.backsystem.aop.SaveOrUpdateEntityAnn;
import com.aaa.backsystem.entity.Dept;
import com.aaa.backsystem.entity.LayuiTable;
import com.aaa.backsystem.entity.Notification;
import com.aaa.backsystem.entity.Result;
import com.aaa.backsystem.service.NotificationService;
import com.aaa.backsystem.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-07-23
 */
@Controller
@RequestMapping("/notification")
public class NotificationController extends BaseController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/toShowNotification")
    public String toShowNotification(){
        return "notic/notic";
    }


    /**
     * 分页查询通知公告列表
     * @param page
     * @param limit
     * @param searchtitle
     * @param searchCreateBy
     * @param beginDate
     * @param endDate
     * @return
     */
    @GetMapping("/notificationList")
    @ResponseBody
    public LayuiTable notificationList(Integer page, Integer limit, String searchtitle,String searchCreateBy, String beginDate,String endDate) {
        LayuiTable layuiTable = new LayuiTable();

        Wrapper wrapper = new EntityWrapper();

        //添加模糊查询
        if (null != searchtitle && !"".equals(searchtitle)) {
            wrapper.like("title", searchtitle);
        }
        if (null != searchCreateBy && !"".equals(searchCreateBy)) {
            wrapper.like("create_by", searchCreateBy);
        }
        if ((null != beginDate && !"".equals(beginDate))&&(null != endDate && !"".equals(endDate))) {
            wrapper.between("create_time",beginDate,endDate );
        }
        wrapper.eq("del_flag", 0);//存在的用户
        int notificationCount = notificationService.selectCount(wrapper);
        //如果表中没有数据，则不进行分页查询
        if (notificationCount > 0) {
            Page<Notification> pageInfo = new Page(page, limit);
            Page<Notification> notificationPage = notificationService.selectPage(pageInfo, wrapper);
            List<Notification> notificationList = notificationPage.getRecords();
            //从分页结果中提取list集合
            layuiTable.setCode(MyConstants.OPERATION_SUCCESS_CODE);
            layuiTable.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
            layuiTable.setData(notificationList);
            layuiTable.setCount(notificationCount);
        }
        return layuiTable;
    }

    /**
     * 保存部门
     * @param notification
     * @return
     */
    @SaveOrUpdateEntityAnn(entityClass = Notification.class)
    @RequestMapping("/saveNotification")
    @ResponseBody
    public Result saveNotification(Notification notification){
        boolean insert = notificationService.insert(notification);
        if(insert){
            return super.success();
        }else{
            return super.error();
        }
    }

    /**
     * 更新部门信息（controller中的方法作用1：页面跳转，2：收受参数，返回参数
     * 不建议在controller中加入过多的业务逻辑）
     * @param notification
     * @return
     */
    @RequestMapping("/updateNotification")
    @ResponseBody
    @SaveOrUpdateEntityAnn(entityClass = Notification.class)
    public Result updateNotification(Notification notification) {
        boolean update = notificationService.updateById(notification);
        if (update) {
            return super.success();
        } else {
            return super.error();
        }

    }


    /**
     * 删除公告
     * @param id
     * @return
     */
    @RequestMapping("/deleteNotice")
    @ResponseBody
    public Result deleteNotice(Integer id){
        boolean delete = notificationService.deleteById(id);
        if (delete){
            return super.success();
        }else {
            return super.error();
        }
    }

    /**
     * 批量删除公告
     */
    @RequestMapping("/deleteBatchNotices")
    @ResponseBody
    public Result deleteBatchNotices(@RequestBody List<Notification> noticeList){
        List list = new ArrayList();
        for (int i = 0; i <noticeList.size() ; i++) {
            list.add(noticeList.get(i).getId());
        }
        boolean deleteBatchIds = notificationService.deleteBatchIds(list);
        if (deleteBatchIds){
            return super.success();
        }else {
            return super.error();
        }
    }

    @GetMapping("/notificationListMain")
    @ResponseBody
    public LayuiTable notificationListMain() {
        LayuiTable layuiTable = new LayuiTable();

        Wrapper<Notification> wrapper = new EntityWrapper();


        List<Notification> notificationList = notificationService.selectList(wrapper);

        layuiTable.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        layuiTable.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        layuiTable.setData(notificationList);

        return layuiTable;
    }

}

